package com.water_work_form.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.water_work_form.entity.*;
import com.water_work_form.mapper.BasicFormMapper;
import com.water_work_form.mapper.RecordFilterMudContentMapper;
import com.water_work_form.service.RecordFilterMudContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 记录——滤料含泥量原始记录 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-08-25
 */
@Service
public class RecordFilterMudContentServiceImpl extends ServiceImpl<RecordFilterMudContentMapper, RecordFilterMudContent> implements RecordFilterMudContentService {

    @Autowired
    RecordFilterMudContentMapper recordFilterMudContentMapper;

    @Autowired
    BasicFormMapper basicFormMapper;

    @Value("${file.templateFilePath}")
    private String templateFilePath;

    @Value("${file.destFilePath}")
    private String destFilePath;

    /**
     * 记录——滤料含泥量原始记录 项目查询（两个查询项+分页+排序）
     * @param sampleSite 取样地点（sample_site） 若非空则模糊查询
     * @param current 分页 当前页
     * @param status 状态 若非空进行查询
     * @param size 分页 每页数据量
     * @return 返回相应数据 按照状态（status）、送样日期（delivery_date）的顺序进行升序排序
     */
    @Override
    public IPage<RecordFilterMudContent> selectRecordFilterMudContent(String sampleSite, String status, Integer current, Integer size){
        QueryWrapper<RecordFilterMudContent> queryWrapper = new QueryWrapper<>();
        if(!sampleSite.isEmpty()){
            queryWrapper.like("sample_site", sampleSite);
        }
        if(!status.isEmpty()){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("status", "delivery_date");
        IPage<RecordFilterMudContent> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    /**
     * 记录——滤料含泥量原始记录 基础表数据查询
     * @param name form表name
     * @return 返回相应数据 表名为键名
     */
    @Override
    public Map<String,Object> selectBasicFilterMudContent(String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("form", basicFormMapper.getOneByName(name));
        return map;
    }

    /**
     * 记录——滤料含泥量原始记录 数据表导出
     * @param recordFilterMudContent 需要导出的数据表
     * @throws IOException 异常
     */
    @Override
    public void downloadRecordFilterMudContent(RecordFilterMudContent recordFilterMudContent) throws IOException {

        Map<String, Object> data = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        data.put("deliveryDate",dateFormat.format(recordFilterMudContent.getDeliveryDate()));
        data.put("deliveryUserName",recordFilterMudContent.getDeliveryUserName());
        data.put("checkDate",dateFormat.format(recordFilterMudContent.getCheckDate()));
        data.put("checkUserName",recordFilterMudContent.getCheckUserName());
        data.put("list",recordFilterMudContent);
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "RecordFilterMudContent.docx");
        compile.render(data);
        compile.writeToFile(destFilePath + recordFilterMudContent.getId() + "out_recordFilterMudContent.docx");
        compile.close();
    }
}
